﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization; 


namespace ExchangeListener
{
    class SendSMS
    {
        private SendSMS()
        {
            oms = new OMSService();
        }

        private static SendSMS ss;

        public static SendSMS Instance
        {
            get
            {
                if (ss == null)
                    ss = new SendSMS();
                return ss;
            }
        }

        private OMSService oms;

        public void Send(string msg)
        {
            Console.WriteLine(msg);
        }

        public void Send(Appointment ap)
        {
            Send(string.Format("事件提醒 主题:{0} 地点{1} 开始时间:{2}", ap.Subject, ap.Location, ap.Start.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")));
        }

        private void CallGWS(string msg)
        {
            tXmsData txd = new tXmsData();
            txd.user = new tUser();
            
            string sendmsg = XmlSerializer<tXmsData>(txd);
            oms.DeliverXms(sendmsg);
        }

        private static string XmlSerializer<T>(T serialObject) where T : class
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Unicode);
            ser.Serialize(writer, serialObject);
            writer.Close();

            return Encoding.Unicode.GetString(mem.ToArray());
        }
    }
}
